<?php

/**
 * Description of LiveRankings
 *
 */

namespace ManiaLivePlugins\MLEPP\Widgets\Widgets\Controlers\Defaults\ManiaRecords;

use ManiaLive\PluginHandler\PluginHandler;
use ManiaLive\Event\Dispatcher;
use ManiaLive\Data\Storage;

class TimeAttack extends \ManiaLivePlugins\MLEPP\Widgets\Widgets\Controlers\AutoHideDuringRace {

    private $mrPlugin;

    public function onInit() {
        parent::onInit();
        $this->setVersion(0.1);
        $this->setWhenToShow(1);
        $this->setCanActivatePerf(true);

        if ($this->isPluginLoaded('MLEPP\ManiaRecords')) {
            Dispatcher::register(\ManiaLivePlugins\MLEPP\ManiaRecords\Events\onDataSync::getClass(), $this);
        }
    }

    public function onLoad() {
        parent::onLoad();
        $this->enableStorageEvents();
        $this->enableDedicatedEvents();
    }

    public function onReady() {
        parent::onReady();
        $this->mrPlugin = \ManiaLivePlugins\MLEPP\ManiaRecords\ManiaRecords::getInstance();
    }

    public function onUnload() {
        unset($this->mrPlugin);
        Dispatcher::unregister(\ManiaLivePlugins\MLEPP\LocalRecords\Events\onDataSync::getClass(), $this);
        parent::onUnload();
    }

    public function onPlayerConnect($login, $isSpectator) {
        //The parent handles the widget we need to call it
        parent::onPlayerConnect($login, $isSpectator);
    }

    public function onPlayerDisconnect($login) {
        //The parent handles the widget we need to call it
        parent::onPlayerDisconnect($login);
    }

    public function onEndMap($rankings, $challenge, $wasWarmUp, $matchContinuesOnNextChallenge, $restartChallenge) {
        parent::onEndMap($rankings, $challenge, $wasWarmUp, $matchContinuesOnNextChallenge, $restartChallenge);
    }

    public function onMLEPP_LocalRecords_newRecord($login, $record, $oldRank) {
        $this->data = $this->mrPlugin->getRecords();
        $this->updateWidget($oldRank, $record->rank, $this->storage->getPlayerObject($login));
        //$this->updateWidgetData($this->lrPlugin->getRecords());
        //$this->forceUpdateWidget();
    }

    public function onMLEPP_ManiaRecords_sync($records) {
        $this->data = array();
        $this->data = $records;
        $this->resetWidgetData();
        $this->updateWidgetData($records);
        $this->inRace = true;
        $this->forceUpdateWidget();
    }

    public function onMLEPP_LocalRecords_firstRecord($login, $record, $oldRank) {
        $this->onMLEPP_LocalRecords_newRecord($login, $record, -1);
    }

    public function onMLEPP_LocalRecords_bestScore($login, $record) {
        
    }

    function destroy() {
        parent::destroy();
        gc_collect_cycles();
    }

}

?>
